Abstract class vs Interfaces

  • Note

    1. What

    Interface and Abstract class are used to achieve the oops feature polymorphism and abstraction.

    2. Difference

    FeaturesAbstract classInterface
    PurposeProvides a base class with shared logicDefines a contract (what must be implemented)
    MethodsCan have abstract (no body) and concrete (with body) methodsOnly method signatures (no body)
    PropertiesCan define properties (variables)Cannot have regular properties
    InheritanceSingle inheritance onlyA class can implement multiple interfaces
    Use Casewhen you need to share code across related classes with comman structurewhen you want to enforce comman structure across unrelated classes
    Access ModifiersCan use public, protected, or privateMethods are always public
    ConstructorCan have constructorsCannot have constructors